package com.iurac.service.impl;

import com.iurac.dao.BookDao;
import com.iurac.domain.Book;
import com.iurac.exception.ManageException;
import com.iurac.service.BookService;
import com.iurac.vo.EchartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("bookService")
public class BookServiceImpl implements BookService {

    @Autowired
    BookDao bookDao;

    @Override
    @Transactional(readOnly = true)
    public List<Book> getBooksByConditionInPage(int page,String info,String[] type,boolean stock) {
        return bookDao.getBooksByConditionInPage(page,info,type,stock);
    }

    @Override
    @Transactional(readOnly = true)
    public Book getBooksById(String id) {
        return bookDao.getBooksById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Book> getBooksByCondition(String title, String press, String author, String minPrice,
                                          String maxPrice, boolean stock, String[] type) {
        return bookDao.getBooksByCondition(title,press,author,minPrice,maxPrice,stock,type);
    }

    @Override
    @Transactional(readOnly = true)
    public EchartsVo getBookTypeCharts() {
        EchartsVo echartsVo = new EchartsVo();
        List<Map<String ,Object>> list = bookDao.getNumOfBookInType();
        int total = bookDao.getNumOfBook();
        echartsVo.setDataList(list);
        echartsVo.setTotal(total);
        return echartsVo;
    }

    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getDateAboutBook() {
        Map<String, Object> map = new HashMap<>();

        Integer numOfBook = bookDao.getNumOfBook();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = sdf.format(date);
        Integer numOfBorrowBookToday = bookDao.getNumOfBorrowBookToday(today);
        Integer numOfBorrowBook = bookDao.getNumOfBorrowBook();
        Integer numOfBorrowBookTotal = bookDao.getNumOfBorrowBookTotal();
        map.put("numOfBook",numOfBook);
        map.put("numOfBorrowBookToday",numOfBorrowBookToday);
        map.put("numOfBorrowBook",numOfBorrowBook);
        map.put("numOfBorrowBookTotal",numOfBorrowBookTotal);
        return map;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean deleteBookById(String id) throws ManageException {
        Book book = bookDao.getBooksById(id);
        if(Integer.parseInt(book.getBorrows())!=0){
            throw new ManageException("该图书有借出");
        }
        int result = bookDao.deleteBookById(id);
        if(result!=1){
            throw new ManageException("删除图书失败");
        }
        return true;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean saveBook(Book book) throws ManageException {
        int bookCount = bookDao.getBookCountByLocation(book.getLocation());
        if(bookCount != 0){
            throw new ManageException("该位置已有图书");
        }
        int result = bookDao.saveBook(book);
        if(result!=1){
            throw new ManageException("添加图书失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = ManageException.class)
    public boolean updateBook(Book book) throws ManageException {
        int bookCount = bookDao.getBookCountByLocation(book.getLocation());
        if(bookCount != 0){
            throw new ManageException("该位置已有图书");
        }
        int result = bookDao.updateBook(book);
        if(result!=1){
            throw new ManageException("更新图书失败");
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Book> getBooksBySerialNumber(String serialNumber) {
        return bookDao.getBooksBySerialNumber(serialNumber);
    }
}
